﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using System.IO;
using System.Net;
using Utilities; 

using ContentProcessor.net.live.search.api;
using Google.GData.Client; 

namespace ContentProcessor
{
    
    public class LinkedInContent : WebContent
    {
        const string AppId = "6DBE504A15F47CC141303AFEBAFF8C776BC25478";
        const string SearchURL = "http://api.search.live.net/xml.aspx"; 

        #region WebContent Members

        public void Search(string keywords, List<ResultItem> results)
        {
            using (LiveSearchService service = new LiveSearchService())
            {
                try
                {
                    SearchDirect(keywords, results);
                }
                catch (System.Web.Services.Protocols.SoapException ex)
                {
                    // A SOAP Exception was thrown. Display error details.
                    //DisplayErrors(ex.Detail);
                }
                catch (System.Net.WebException ex)
                {
                    // An exception occurred while accessing the network.
                    Console.WriteLine(ex.Message);
                }
            }
        }

        #endregion

        private void SearchDirect(string keywords, List<ResultItem> results)
        {
            //http://api.search.live.net/xml.aspx?AppId=6DBE504A15F47CC141303AFEBAFF8C776BC25478&
            //Market=en-US&Query=James%20Brown%20(site:www.linkedin.com)&Sources=web+spell&Web.Count=10&Web.Offset=0&xmltype=elementbased
            string QueryURL = SearchURL + "?" +
                              "AppId=" + AppId + "&" +
                              "Market=en-US&" +
                              "Query=" + keywords + " (site:www.linkedin.com)&" +
                              "Sources=web+spell&" +
                              "Web.Count=20&Web.Offset=0&xmltype=elementbased";
            try
            {
                HttpWebRequest loginRequest = (HttpWebRequest)System.Net.WebRequest.Create(QueryURL);

                loginRequest.Credentials = CredentialCache.DefaultCredentials;
                loginRequest.Proxy = Google.GData.Client.Utilities.CommonOperations.GetProxy();
                loginRequest.Method = "GET";

                HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse();

                Stream receiveStream = loginResponse.GetResponseStream();
                XmlTextReader readStream = new XmlTextReader(receiveStream);

                ParseResults(readStream, results); 

                //string s = readStream.ReadToEnd();
                //Console.Write(readStream.ReadToEnd());

                loginResponse.Close();
                receiveStream.Close();
            }
            catch (WebException ex)
            {
                System.Diagnostics.Trace.WriteLine("SearchDirect error: " + ex.StackTrace);
            }
        }


        private void ParseResults(XmlTextReader reader, List<ResultItem> results)
        {
            ResultItem item = null; 
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "web:Total")
                    {
                        reader.Read();
                        if (reader.HasValue)
                        {
                            if (reader.Value.Equals("0")) return;
                        }
                    }
                    else if (reader.Name == "web:WebResult")
                    {
                        item = new ResultItem();
                        results.Add(item);
                    }
                    else if (reader.Name == "web:Title")
                    {
                        reader.Read();
                        if (reader.HasValue)
                        {
                            item.Title = reader.Value; 
                        }
                    }
                    else if (reader.Name == "web:Description")
                    {
                        reader.Read();
                        if (reader.HasValue)
                        {
                            item.Description = reader.Value;
                        }
                    }
                    else if (reader.Name == "web:Url")
                    {
                        reader.Read();
                        if (reader.HasValue)
                        {
                            item.URL = reader.Value;
                        }
                    }
                    else if (reader.Name == "web:DisplayUrl")
                    {
                        reader.Read();
                        if (reader.HasValue)
                        {
                            item.DisplayURL = reader.Value;
                        }
                    }
                }
            }

        }

        /*
        private SearchRequest BuildRequest(string keywords)
        {
            SearchRequest request = new SearchRequest();

            // Common request fields (required)
            request.AppId = AppId;
            request.Query = keywords + "%20(site:www.linkedin.com)";
            request.Sources = new SourceType[] { SourceType.Web, SourceType.Spell };

            // Common request fields (optional)
            request.Version = "2.0";
            request.Market = "en-us";
            request.Adult = AdultOption.Moderate;
            request.AdultSpecified = true;
            request.Options = new ContentProcessor.net.live.search.api.SearchOption[]
            {
                ContentProcessor.net.live.search.api.SearchOption.EnableHighlighting
               
            };
            

            // Web-specific request fields (optional)
            request.Web = new ContentProcessor.net.live.search.api.WebRequest();
            request.Web.Count = 10;
            request.Web.CountSpecified = true;
            request.Web.Offset = 0;
            request.Web.OffsetSpecified = true;
            request.Web.FileType = "DOC";
            request.Web.Options = new WebSearchOption[]
            {
                WebSearchOption.DisableHostCollapsing,
                WebSearchOption.DisableQueryAlterations
            };
            

            return request;
        }

        static void BuildResponse(SearchResponse response, List<ResultItem> results)
        {
            foreach (WebResult result in response.Web.Results)
            {
                ResultItem item = new ResultItem();
                item.Title = result.Title;
                item.Description = result.Description;
                item.URL = result.Url;
                results.Add(item);
            }
        }
        */ 

    }
}
